﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Configuration;

namespace WebApplication1
{
    public partial class log : System.Web.UI.Page
    {
        SqlConnection cnn = new SqlConnection(WebConfigurationManager.AppSettings["connect"]);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtPwd.Text = "";
                txtUname.Text = "";
            }
        }

        protected void btnLogin_click(object sender, EventArgs e)
        {
            string name = txtUname.Text;
            string pwd = txtPwd.Text;
            if (name.Equals("admin") && pwd.Equals("admin"))
            {
                Session.Add("name", "admin");
                Response.Redirect("~/Admin/default1.aspx");
            }
            else
            {
                cnn.Open();
                SqlCommand cmd = new SqlCommand("select uname,pass,no from regstr", cnn);
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    if (name.Equals(dr["uname"].ToString()) && pwd.Equals(dr["pass"].ToString()))
                    {
                        Session.Add("no", dr["no"].ToString());
                        Session.Add("name", dr["uname"].ToString());
                        Response.Redirect("~/Users/usracc.aspx");
                    }
                }
                Response.Write("<script>alert('Please Register and then login')");
                Response.Redirect("~/Users/reg.aspx");
            }
        }

        protected void clear_Click(object sender, EventArgs e)
        {
            txtPwd.Text = "";
            txtUname.Text = "";
        }


    }
}